/**
*Doubly LinkedList used to hold the docId's and the score.
*
*/
class PostingsList {
	
	private Posting head;
	private Posting tail;
	private int size;
	/**
	*Internal class for the postings
	*@param docId 
	*@param count number of time the term is found in the Document
	*@param next the next posting in the List
	*@param prev the previous posting in the list
	*/
	private class Posting{
		private String docId;
		private int count;
		private Posting next;
		private Posting prev;
		
		public Posting(){
			this.docId=null;
			this.count=0;
			this.next=null;
			this.prev=null;
		}
		public Posting(Posting Posting){
			this.docId=Posting.docId;
			this.count=Posting.count;
			this.next=Posting.next;
			this.prev=Posting.prev;
		}
		public Posting(String docId, Posting next, Posting prev){
			this.docId=docId;
			this.next=next;
			this.prev=prev;
			this.count=1;
		}

	
	
		public int getCount(){
			return this.count;
		}
		public String getDocId(){
			return docId;
		}
		public void setDocId(String id){
			
		}
		public void increaseCount(){
			this.count++;	
		}
	}
	public boolean contains(Posting matcher)
	{
		if (this.find(matcher)!=null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public PostingsList(String docId){
		head=new Posting(docId, null, null);
		tail=null;
		size=1;
	}
	public String displayContents(){
		Posting temp=new Posting();
		temp=head;
		String listContents="";
		while(temp!=null){
		        listContents += ("\n");
			listContents += ("\tDoc: "+temp.getDocId()+" Count:{"+temp.getCount()+"} ");
			temp=temp.next;
		}
		return listContents;
	}
	
	public PostingsList mergeTwoLists(PostingsList mergingList)
	{
		Posting temp=new Posting();
		Posting iterator=new Posting(head);
		while (iterator.next!=null)
		{
			temp=mergingList.find(iterator);
			if(temp!=null)
			{
				iterator.count=iterator.getCount()+temp.getCount();
			}
		}
		temp=null;
		return this;
	}
	
	public Posting find(Posting matcher)
		{
		Posting iterator=new Posting(head);
		while (iterator.getDocId()!=matcher.getDocId()&&iterator.next!=null)
			{
				iterator=iterator.next;
			}
			if (iterator.getDocId()==matcher.getDocId())
			{
				return iterator;
			}
			else return null;
		}
	public void addToPostingList(String dId){
		Posting temp=new Posting();
		temp=head;
		boolean found=false;
		Posting tempPrev=new Posting();
		while(temp!=null){
			if(temp.getDocId().equals(dId)){
				temp.increaseCount();
				found=true;
			}
			Posting temp2=new Posting(temp);
			tempPrev=temp;
			temp2.prev=temp;
			temp2=temp.next;
			temp=temp2;
			temp2=null;
		}
		if(!found){
			temp=new Posting(dId, null, tempPrev);
			tempPrev.next=temp;
			tail=temp;
			size++;
			}
	}

}
